package com.wyc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyc.dto.Result;
import com.wyc.dto.UserDTO;
import com.wyc.entity.Follow;
import com.wyc.mapper.FollowMapper;
import com.wyc.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyc.service.IUserService;
import com.wyc.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *

 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Resource
    IUserService userService;

    @Override
    public Result followCommons(Long id) {
        // 获取当前用户ID
        Long userId = UserHolder.getUser().getId();
        String key = "follows:" + userId;
        // 求交集
        String key2 = "follows:" + id;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key, key2);
        if (intersect == null || intersect.isEmpty()) {
            // 没有交集代表为空
            return Result.ok(Collections.emptyList());
        }
        // 解析ID集合
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        // 查询用户
        List<UserDTO> users = userService.listByIds(ids)
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(users);
    }

    @Override
    public Result follow(Long FollowId, boolean isFollow) {
        UserDTO user = UserHolder.getUser();
        // 说明用户未登录
        if (user == null) {
            return Result.ok(Collections.emptyList());
        }
        // 获取登录用户
        Long userId = user.getId();
        String key = "follows:" + userId;
        // 判断是关注还是取关
        if (isFollow){
            // 没关注，新增
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(FollowId);
            boolean isSuccess = save(follow);
            if (isSuccess){
                // 把关注的ID 放入redis的 set集合 sadd

                stringRedisTemplate.opsForSet().add(key,FollowId.toString());
            }
        }else {
            // 取关
            boolean isSuccess = remove(new QueryWrapper<Follow>().eq("user_id", userId).eq("follow_user_id", FollowId));
            if (isSuccess){
                // 把取关的ID 从redis的 set集合 删除
                stringRedisTemplate.opsForSet().remove(key,FollowId.toString());
            }
        }
        return Result.ok();
    }

    @Override
    public Result isFollow(Long FollowId) {

        UserDTO user = UserHolder.getUser();
        // 用户未登录
        if (user == null) {
            return Result.ok();
        }
        // 获取登录用户
        Long userId = user.getId();
        Integer count = query().eq("user_id", userId).eq("follow_user_id", FollowId).count();
        return Result.ok(count > 0);
    }
}
